package site.shader85.OICQBind;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.bukkit.configuration.file.YamlConfiguration;

public class XlsWriter {
	YamlConfiguration data;
	BindPlugin bp;
	public XlsWriter(YamlConfiguration yml,BindPlugin b){
		data = yml;
		bp = b;
	}
	public void write(String name){
		 Workbook wb = null; 
	     Sheet sheet =null;
	     wb = new HSSFWorkbook();
	     sheet = (Sheet) wb.createSheet("datas");
	     Row row = sheet.createRow(0);
	     Cell cell = row.createCell(0);
	     cell.setCellType(Cell.CELL_TYPE_STRING);
	     cell.setCellValue("玩家id");
	     cell = row.createCell(1);
	     cell.setCellType(Cell.CELL_TYPE_STRING);
	     cell.setCellValue("QQ号码");
	     sheet.setColumnWidth(1, 15 * 256);
	     Set<String> hs = data.getKeys(false);
	     int num = 1;
	     for(String n:hs){
	    	 Row w = sheet.createRow(num);
	    	 Cell c = w.createCell(0);
	    	 c.setCellType(Cell.CELL_TYPE_STRING);
	    	 c.setCellValue(n);
	    	 c = w.createCell(1);
	    	 c.setCellType(Cell.CELL_TYPE_STRING);
	    	 c.setCellValue(data.getString(n));
	    	 num++;
	     }
	     try{
		     File file = new File(bp.getDataFolder(),name);
		     if(file.exists())
		    	 file.delete();
		     FileOutputStream fileOut = new FileOutputStream(file);
		     BufferedOutputStream bos = new BufferedOutputStream(fileOut);
		     wb.write(bos);
		     bos.flush();
		     bos.close();
	     }catch(Exception e){}
	}
}
